package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Category;
import com.lin.pojo.Promotion;
import com.lin.pojo.Wares;
import com.lin.services.CategoryService;
import com.lin.services.PromotionService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:PromotionController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/20
 * ♣♣♣  促销活动    --存在大量代码可优化
 */

@Controller
@RequestMapping("/promotion")
public class PromotionController {
    @Autowired
    private PromotionService promotionService;
    @Autowired
    private CategoryService categoryService;

    @MyLog("访问促销活动")
    @RequiresPermissions("promotion:promotion:list")
    @RequestMapping(value = "/toPromotion", method = RequestMethod.GET)
    public String toPromotion(HttpServletRequest request) {
        request.setAttribute("ddId", "Promotion");
        return "promotionMain";
    }

    @RequiresPermissions("promotion:promotion:insert")
    @RequestMapping(value = "/promotionChoose", method = RequestMethod.POST)
    public String createPromotion(HttpSession session, HttpServletRequest request) {
        request.setAttribute("ddId", "Promotion");
        String type = request.getParameter("type");
        if (type != null) {
            if ("限时折扣".equals(type)) {
                request.setAttribute("promotionType", "限时折扣");
                return "promotionAdd";
            }
            if ("限时满减".equals(type)) {
                request.setAttribute("promotionType", "限时满减");
                return "promotionAdd";
            }
            if ("特价商品".equals(type)) {
                request.setAttribute("promotionType", "特价商品");
                return "promotionAdd";
            }
            if ("特价分类".equals(type)) {
                List<Category> list = categoryService.getCategory1();
                request.setAttribute("promotionType", "特价分类");
                request.setAttribute("Category1", list);
                return "promotionAdd";
            }
        }
        return "promotionMain";
    }

    @RequiresPermissions("promotion:promotion:list")
    @RequestMapping(value = "/getPromotionWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPromotionWares(HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        Date endDate = sdf.parse(request.getParameter("endDate"));
        Integer promotionId = Integer.parseInt(request.getParameter("promotionId"));
        String data = request.getParameter("data");
        List<Wares> list = promotionService.getPromotionWares(promotionId, endDate, data);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("promotion:promotion:list")
    @RequestMapping(value = "/getPromotionWaresById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPromotionWaresById(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String waresId = request.getParameter("waresIdOrName");
        List<Wares> list = promotionService.findPromotionWaresById(waresId);
        return PageUnit.Page(list, page, limit);
    }

    @MyLog("创建促销活动")
    @RequiresPermissions("promotion:promotion:insert")
    @RequestMapping(value = "/insertPromotion", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertPromotion(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        System.out.println(map.toString());
        return promotionService.insertPromotion(map);
    }

    @RequestMapping(value = "/insertPromotionWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertPromotionWares(HttpServletRequest request) {
        String waresId = request.getParameter("waresId");
        String promotionId = request.getParameter("promotionId");
        return promotionService.insertPromotionWares(promotionId, waresId);
    }

    @RequestMapping(value = "/deletePromotionWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deletePromotionWares(HttpServletRequest request) {
        String waresId = request.getParameter("waresId");
        String promotionId = request.getParameter("promotionId");
        return promotionService.deletePromotionWares(promotionId, waresId);
    }

    @RequiresPermissions("promotion:promotion:list")
    @RequestMapping(value = "/promotionInfoSearch", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findPromotionInfo(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        Map<String, String[]> condition = request.getParameterMap();
        List<Promotion> lists = promotionService.getPromotion(condition);
        Map<String, Object> map = new HashMap<>();
        if (condition.size() <= 2) {
            map = PageUnit.Page(lists, page, limit);
        } else {
            map.put("code", true);
            map.put("data", lists);
        }
        return map;
    }

    @MyLog("结束/删除活动")
    @RequiresPermissions("promotion:promotion:delete")//删除/结束活动
    @RequestMapping(value = "/promotionInfoTool", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deletePromotion(HttpServletRequest request) {
        String promotionId = request.getParameter("promotionId");
        String toolType = request.getParameter("toolType");
        return promotionService.promotionOperation(promotionId, toolType);
    }

    @MyLog("查看活动详情")
    @RequiresPermissions("promotion:promotion:list")
    @RequestMapping(value = "showPromotionDetails", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPromotionById(HttpServletRequest request) {
        String promotionId = request.getParameter("promotionId");
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "查询失败");
        if (promotionId != null && !"".equals(promotionId.trim())) {
            List<Map<String, Object>> list = promotionService.getPromotionDetails(promotionId);
            map.put("ruleData", list);
            map.put("code", true);
        }
        return map;
    }
}

